package v2

import (
	"github.com/silenceper/wechat/v2/pay"
	wxpayCfg "github.com/silenceper/wechat/v2/pay/config"
	wxpayOrder "github.com/silenceper/wechat/v2/pay/order"
	"strconv"
	"time"
)

type WxPay struct {
	*pay.Pay
}

func NewWxPay(cfg *wxpayCfg.Config) WxPay {
	return WxPay{
		Pay: pay.NewPay(cfg),
	}
}

func (wx *WxPay) PrePayOrder(p OrderParams) (wxpayOrder.Config, error) {

	current := time.Now()
	expireTime := current.Add(time.Minute * 15)

	return wx.GetOrder().BridgeConfig(&wxpayOrder.Params{
		TotalFee:   strconv.FormatInt(int64(p.Money), 10),
		CreateIP:   p.CreateIp,
		Body:       p.GoodsName,
		OutTradeNo: p.OrderNum,
		TimeExpire: expireTime.Format("20060102150405"),
		OpenID:     p.OpenId,
		TradeType:  p.TradeType,
		SignType:   "MD5",
		Detail:     p.Detail,
		Attach:     p.Attach,
		GoodsTag:   p.GoodsTag,
	})
}

type OrderParams struct {
	Money     int
	CreateIp  string
	OrderNum  string
	TradeType string
	GoodsName string
	OpenId    string
	Attach    string
	Detail    string
	GoodsTag  string
}
